package com.nulldev.util.graphics.rast2d.draw;

import java.awt.Color;
import java.awt.Composite;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.RenderingHints.Key;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ImageObserver;
import java.awt.image.RenderedImage;
import java.awt.image.renderable.RenderableImage;
import java.text.AttributedCharacterIterator;
import java.util.Map;

import com.nulldev.util.VariableAPI.MathUtil;
import com.nulldev.util.data.Variables;
import com.nulldev.util.data.Arrays.maps.FastMaps;
import com.nulldev.util.graphics.rast2d.Pixmap;

public class Rast2DGraphics extends Graphics2D {

	private final Pixmap pixmap;
	private volatile int global_color;

	public Rast2DGraphics(final int width, final int height) {
		this(new Pixmap(width, height, Pixmap.PIXMAP_FMT_RGB888));
	}

	public Rast2DGraphics(final Pixmap pixmap) {
		super();
		Variables.requireNonNull(pixmap, "pixmap");
		this.pixmap = pixmap;
	}

	@Override
	public Graphics create() {
		return this;
	}

	@Override
	protected Object clone() throws CloneNotSupportedException {
		final Pixmap pm = new Pixmap(this.pixmap.pixmapWidth(), this.pixmap.pixmapHeight(), this.pixmap.pixmapFormat());
		pm.importPixmap(this.pixmap);
		return new Rast2DGraphics(pm);
	}

	@Override
	public void translate(int x, int y) {
		// TODO Auto-generated method stub

	}

	@Override
	public Color getColor() {
		return new Color(this.global_color);
	}

	@Override
	public void setColor(final Color c) {
		if (c == null)
			return;
		this.global_color = c.getRGB();
	}

	@Override
	public void setPaintMode() {
		// TODO Auto-generated method stub

	}

	@Override
	public void setXORMode(Color c1) {
		// TODO Auto-generated method stub

	}

	@Override
	public Font getFont() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setFont(Font font) {
		// TODO Auto-generated method stub

	}

	@Override
	public FontMetrics getFontMetrics(Font f) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Rectangle getClipBounds() {
		return new Rectangle(0, 0, this.pixmap.pixmapWidth(), this.pixmap.pixmapHeight());
	}

	@Override
	public void clipRect(int x, int y, int width, int height) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setClip(int x, int y, int width, int height) {
		// TODO Auto-generated method stub

	}

	@Override
	public Shape getClip() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setClip(Shape clip) {
		// TODO Auto-generated method stub

	}

	@Override
	public void copyArea(final int x, final int y, final int width, final int height, final int dx, final int dy) {
		this.pixmap.copy(x, y, width, height, dx, dy);
	}

	@Override
	public void drawLine(int x1, int y1, int x2, int y2) {
		this.pixmap.line(x1, y1, x2, y2, this.global_color);
	}

	@Override
	public void drawRect(int x, int y, int width, int height) {
		this.pixmap.rect(x, y, width, height, this.global_color);
	}

	@Override
	public void fillRect(int x, int y, int width, int height) {
		this.pixmap.fill(x, y, width, height, this.global_color);
	}

	@Override
	public void clearRect(final int x, final int y, final int width, final int height) {
		if ((x == 0 && y == 0) /* clearRect(): FAST */
				&& (width == this.pixmap.pixmapWidth() && height == this.pixmap.pixmapHeight())) {
			this.pixmap.clear();
		} else { /* clearRect(): SLOW */
			final int tw = MathUtil.clamp(0, this.pixmap.pixmapWidth(), width);
			final int th = MathUtil.clamp(0, this.pixmap.pixmapHeight(), height);
			this.pixmap.fill(x, y, tw, th, Integer.MAX_VALUE);
		}
	}

	@Override
	public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) {
		// TODO Auto-generated method stub

	}

	@Override
	public void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) {
		// TODO Auto-generated method stub

	}

	@Override
	public void drawOval(int x, int y, int width, int height) {
		// TODO Auto-generated method stub

	}

	@Override
	public void fillOval(int x, int y, int width, int height) {
		// TODO Auto-generated method stub

	}

	@Override
	public void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) {
		// TODO Auto-generated method stub

	}

	@Override
	public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) {
		// TODO Auto-generated method stub

	}

	@Override
	public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints) {
		// TODO Auto-generated method stub

	}

	@Override
	public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints) {
		// TODO Auto-generated method stub

	}

	@Override
	public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints) {
		// TODO Auto-generated method stub

	}

	@Override
	public void drawString(String str, int x, int y) {
		// TODO Auto-generated method stub

	}

	@Override
	public void drawString(AttributedCharacterIterator iterator, int x, int y) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean drawImage(final Image img, final int x, final int y, final ImageObserver observer) {
		Variables.requireNonNull(img, "img");
		return this.drawImage(img, x, y, img.getWidth(observer), img.getHeight(observer), observer);
	}

	@Override
	public boolean drawImage(final Image img, final int x, final int y, final int width, final int height, final ImageObserver observer) {
		Variables.requireNonNull(img, "img");
		final BufferedImage bi = (BufferedImage) img;
		for (int currentY = 0; currentY < height; currentY++) {
			final int[] pix = bi.getRGB(currentY, currentY, width, height, null, 0, 0);
			this.pixmap.writeLine(x, currentY, pix);
		}
		return false;
	}

	@Override
	public boolean drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void dispose() {
	}

	@Override
	public void draw(Shape s) {
	}

	@Override
	public boolean drawImage(Image img, AffineTransform xform, ImageObserver obs) {
		return false;
	}

	@Override
	public void drawImage(BufferedImage img, BufferedImageOp op, int x, int y) {
	}

	@Override
	public void drawRenderedImage(RenderedImage img, AffineTransform xform) {
	}

	@Override
	public void drawRenderableImage(RenderableImage img, AffineTransform xform) {
	}

	@Override
	public void drawString(String str, float x, float y) {
	}

	@Override
	public void drawString(AttributedCharacterIterator iterator, float x, float y) {
	}

	@Override
	public void drawGlyphVector(GlyphVector g, float x, float y) {
	}

	@Override
	public void fill(Shape s) {
	}

	@Override
	public boolean hit(Rectangle rect, Shape s, boolean onStroke) {
		return false;
	}

	@Override
	public GraphicsConfiguration getDeviceConfiguration() {
		return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
	}

	@Override
	public void setComposite(Composite comp) {
	}

	@Override
	public void setPaint(Paint paint) {
	}

	@Override
	public void setStroke(Stroke s) {
	}

	final Map<Key, Object> hints = FastMaps.uniMap(new Object[][]
		{
				{ RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE } });

	@Override
	public void setRenderingHint(final Key hintKey, final Object hintValue) {
		hints.put(hintKey, hintValue);
	}

	@Override
	public Object getRenderingHint(final Key hintKey) {
		return hints.get(hintKey);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void setRenderingHints(final Map<?, ?> _hints) {
		this.hints.putAll((Map<? extends Key, ? extends Object>) _hints);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void addRenderingHints(final Map<?, ?> _hints) {
		this.hints.putAll((Map<? extends Key, ? extends Object>) _hints);
	}

	@Override
	public RenderingHints getRenderingHints() {
		return new RenderingHints(this.hints);
	}

	@Override
	public void translate(double tx, double ty) {
	}

	@Override
	public void rotate(double theta) {
	}

	@Override
	public void rotate(double theta, double x, double y) {
	}

	@Override
	public void scale(double sx, double sy) {
	}

	@Override
	public void shear(double shx, double shy) {
	}

	@Override
	public void transform(AffineTransform Tx) {
	}

	@Override
	public void setTransform(AffineTransform Tx) {
	}

	@Override
	public AffineTransform getTransform() {
		return null;
	}

	@Override
	public Paint getPaint() {
		return null;
	}

	@Override
	public Composite getComposite() {
		return null;
	}

	@Override
	public void setBackground(Color color) {
	}

	@Override
	public Color getBackground() {
		return Color.black;
	}

	@Override
	public Stroke getStroke() {
		return null;
	}

	@Override
	public void clip(Shape s) {
	}

	@Override
	public FontRenderContext getFontRenderContext() {
		return null;
	}
}
